package com.pzhuweb.sgblog.handler.exception;

import com.pzhuweb.sgblog.domain.enums.AppHttpCodeEnum;
import com.pzhuweb.sgblog.exception.SystemException;
import com.pzhuweb.sgblog.domain.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 鄧浩奇
 * @classname GlobalExceptionHandler
 * @description 全局异常处理器
 * @date 2023/6/19 9:50
 **/

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    public ResponseResult<?> systemExceptionHandler(SystemException e) {
        // 打印异常信息
        log.error("出现了异常....",e);
        // 从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(BadCredentialsException.class) // 用户名密码错误
    public ResponseResult<?> loginExceptionHandler(Exception e) {
        // 打印异常信息
        log.error("出现了异常....",e);
        // 从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<?> exceptionHandler(Exception e) {
        // 打印异常信息
        log.error("出现了异常....",e);
        // 从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }


}
